#include <iostream>
#include <sys/socket.h>
#include <string>
#include <cstring>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>

int main()
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(9090);
    ::inet_pton(AF_INET, "124.223.56.190", &addr.sin_addr);
    socklen_t len = sizeof(addr);
    int ret = ::bind(sockfd, (struct sockaddr *)&addr, len);

    ret = ::connect(sockfd, (struct sockaddr *)&addr, len);
    if (ret < 0)
        exit(-1);
    std::cout << "连接成功" << std::endl;

    char buffer[4096];
    while (true)
    {
        std::cout << "enter#: " << std::endl;
        std::string message;
        getline(std::cin, message);
        write(sockfd,message.c_str(),message.size());
        ssize_t n = read(sockfd, &buffer, sizeof(buffer) - 1);
        if (n < 0)
        {
            std::cout << "read fail!" << std::endl;
            break;
        }
        else
        {
            buffer[n] = 0;
            std::cout << buffer << std::endl;
        }
    }
    return 0;
}